package com.platform.common.utils.netty;

import io.netty.buffer.ByteBuf;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class NettyUtil {
    /**
     * 截取字符串 从第几位开始，截取几位
     * @param str1
     * @param start
     * @param length
     * @return 字符数组
     */
    public static String[] strsChange(String str1[],  int start, int length){

        String str2[] = new String[length];
        int k = start + length;
        for(int i = start, j=0 ; i<k && j<length; i++,j++){
            str2[j] = str1[i];
        }
        return str2;
    }

    /**
     * 截取字符串 从第几位开始，截取几位
     * @param str1
     * @param start
     * @param length
     * @return 字符串
     */
    public static String strChange(String str1[],  int start, int length){
        StringBuffer str2 = new StringBuffer();
        int k = start + length;
        for(int i = start; i<k; i++){
            str2.append(str1[i]).append(" ");
        }
        return str2.toString();
    }

    /**
     * @Title:hexString2Bytes
     * @Description:16进制字符串转字节数组
     * @param src 16进制字符串
     * @return 字节数组
     * @throws
     */
    public static byte[] hexString2Bytes(String src) {
        int l = src.length() / 2;
        byte[] ret = new byte[l];
        for (int i = 0; i < l; i++) {
            ret[i] = Integer.valueOf(src.substring(i * 2, i * 2 + 2), 16).byteValue();
        }
        return ret;
    }

    /**
     * 十六进制转2进制字符串
     **/
    public static String hexStringBinStr(String hexStr) {
        int num = Integer.valueOf(hexStr, 16);
        String str = Integer.toBinaryString(num);
        for(int i=str.length(); i < 32; i++){
            str = "0" + str;
        }
        char[] strChar = str.toCharArray();
        String result = "";
        for(int i=0;i<strChar.length;i++){
            result += strChar[i] + " ";
        }
        return result;
    }

    /**
     * 二进制转十进制字符串
     * @param num
     * @return
     */
    public static Integer get10Num(String num){
        return Integer.valueOf(num, 2);
    }

    /**
     * 十六进制转十进制
     * @param num
     * @return
     */
    public static Integer get10HexNum(String num){
        return Integer.valueOf(num, 16);
    }

    /**
     * 十进制转十六进制
     * @param num
     * @return
     */
    public static String get16HexNum(int num){
        return Integer.toHexString(num).toUpperCase();
    }

    /**
     * 16进制直接转换成为字符串(无需Unicode解码)
     * @param hexStr
     * @return
     */
    public static String hexStr2Str(String hexStr) {
        String str = "0123456789ABCDEF";
        char[] hexs = hexStr.toCharArray();
        byte[] bytes = new byte[hexStr.length() / 2];
        int n;
        for (int i = 0; i < bytes.length; i++) {
            n = str.indexOf(hexs[2 * i]) * 16;
            n += str.indexOf(hexs[2 * i + 1]);
            bytes[i] = (byte) (n & 0xff);
        }
        return new String(bytes);
    }

    /**
     * bytebuf转string
     * @param msg
     * @return
     */
    public static String hexByteStr(ByteBuf msg) {
        String HEXES = "0123456789ABCDEF";
        byte[] req = new byte[msg.readableBytes()];
        msg.readBytes(req);
        final StringBuilder hex = new StringBuilder(2 * req.length);

        for (int i = 0; i < req.length; i++) {
            byte b = req[i];
            if(i>0){
                hex.append(" ");
            }
            hex.append(HEXES.charAt((b & 0xF0) >> 4))
                    .append(HEXES.charAt((b & 0x0F)));
        }
        return new String(hex);
    }

    /**
     * 字节数组转float
     * 采用IEEE 754标准
     * @param bytes
     * @return
     */
    public static float bytes2Float(byte[] bytes){
        //获取 字节数组转化成的2进制字符串
        String BinaryStr = bytes2BinaryStr(bytes);
        //符号位S
        Long s = Long.parseLong(BinaryStr.substring(0, 1));
        //指数位E
        Long e = Long.parseLong(BinaryStr.substring(1, 9),2);
        //位数M
        String M = BinaryStr.substring(9);
        float m = 0,a,b;
        for(int i=0;i<M.length();i++){
            a = Integer.valueOf(M.charAt(i)+"");
            b = (float) Math.pow(2, i+1);
            m =m + (a/b);
        }
        Float f = (float) ((Math.pow(-1, s)) * (1+m) * (Math.pow(2,(e-127))));
        return f;
    }

    /**
     * 将字节数组转换成2进制字符串
     * @param bytes
     * @return
     */
    public static String bytes2BinaryStr(byte[] bytes){
        StringBuffer binaryStr = new StringBuffer();
        for(int i=0;i<bytes.length;i++){
            String str = Integer.toBinaryString((bytes[i] & 0xFF) + 0x100).substring(1);
            binaryStr.append(str);
        }
        return binaryStr.toString();
    }
}
